package org.fcitmuk.openclinica;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;

import org.fcitmuk.db.util.AbstractRecord;
import org.fcitmuk.db.util.PersistentHelper;

/**
 * 
 * @author Daniel
 *
 */
public class Subject extends AbstractRecord{
	Integer subjectId; //subjectid is made Integer instead of int because new subjects dont have it.
	String prefix;
	String familyName;
	String middleName;
	String givenName;
	String gender;
	Date birthDate;
	String subjectIdentifier;
	
	/** true if the subject has just been created on the device before being saved on the server? **/
	boolean isNewSubject;
	
	
	public Subject(){
		super();
	}
	
	public Subject(Integer subjectId, String prefix, String familyName, String middleName, String givenName, String gender, Date birthDate, String subjectIdentifier, boolean isNewSubject) {
		this();
		setSubjectId(subjectId);
		setPrefix(prefix);
		setFamilyName(familyName);
		setMiddleName(middleName);
		setGivenName(givenName);
		setGender(gender);
		setBirthDate(birthDate);
		setSubjectIdentifier(subjectIdentifier);
		setNewSubject(isNewSubject);
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public String getFamilyName() {
		return familyName;
	}

	public void setFamilyName(String familyName) {
		this.familyName = familyName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getGivenName() {
		return givenName;
	}

	public void setGivenName(String givenName) {
		this.givenName = givenName;
	}

	public boolean isNewSubject() {
		return isNewSubject;
	}

	public void setNewSubject(boolean isNewSubject) {
		this.isNewSubject = isNewSubject;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public Integer getSubjectId() {
		//For now, new subjects have negative ids. assuming server does not assign negatives
		//We use recordid value because is takes care of generating new ids for us.
		if(isNewSubject())
			;//return new Integer(-getRecordId());
		
		return subjectId;
	}

	public void setSubjectId(Integer subjectId) {
		this.subjectId = subjectId;
	}

	public String getSubjectIdentifier() {
		return subjectIdentifier;
	}

	public void setSubjectIdentifier(String subjectIdentifier) {
		this.subjectIdentifier = subjectIdentifier;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	/**
	 * Gets the subject whole name which is a concatenation of the
	 * given, middle and family names.
	 * 
	 * @return
	 */
	public String getName(){
		String s="";

		if(getGivenName() != null && getGivenName().length() != 0)
			s += " " + getGivenName();
		
		if(getMiddleName() != null && getMiddleName().length() != 0)
			s += " " + getMiddleName();

		if(getFamilyName() != null && getFamilyName().length() != 0)
			s += " " + getFamilyName();

		return s;
	}
	
	public String toString() {
		String s;

		if(getPrefix() != null && getPrefix().length() != 0)
			s = getSubjectIdentifier() + " " + getPrefix();
		else
			s = getSubjectIdentifier();

		if(getGivenName() != null && getGivenName().length() != 0)
			s += " " + getGivenName();
		
		if(getMiddleName() != null && getMiddleName().length() != 0)
			s += " " + getMiddleName();

		if(getFamilyName() != null && getFamilyName().length() != 0)
			s += " " + getFamilyName();

		if(isNewSubject())
			s += " (NEW)";

		if(s == null)
			s = "NAMELESS SubjectId="+getSubjectId();
		return s;
	}

	public void read(DataInputStream dis) throws IOException, InstantiationException, IllegalAccessException {
		setSubjectId(PersistentHelper.readInteger(dis));
		setPrefix(PersistentHelper.readUTF(dis));
		setFamilyName(PersistentHelper.readUTF(dis));
		setMiddleName(PersistentHelper.readUTF(dis));
		setGivenName(PersistentHelper.readUTF(dis));
		setGender(PersistentHelper.readUTF(dis));
		setBirthDate(PersistentHelper.readDate(dis));
		setSubjectIdentifier(PersistentHelper.readUTF(dis));
		setNewSubject(dis.readBoolean());
	}

	public void write(DataOutputStream dos) throws IOException {
		PersistentHelper.writeInteger(dos,getSubjectId());
		PersistentHelper.writeUTF(dos, getPrefix());
		PersistentHelper.writeUTF(dos, getFamilyName());
		PersistentHelper.writeUTF(dos, getMiddleName());
		PersistentHelper.writeUTF(dos, getGivenName());
		PersistentHelper.writeUTF(dos, getGender());
		PersistentHelper.writeDate(dos, getBirthDate());
		PersistentHelper.writeUTF(dos, getSubjectIdentifier());
		dos.writeBoolean(isNewSubject());
	}

}
